﻿using HYJHouseKeeping.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HYJHouseKeeping.DAL
{
    /// <summary>
    /// 员工Service
    /// </summary>
    public class EmployeeService
    {
        //HYJHousekeepingEntities hyjhouse = new HYJHousekeepingEntities();

        #region 员工管理
        /// <summary>
        /// 根据编号查询员工对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns>员工对象</returns>
        public Employee GetEmployeeById(int id)
        {
            using (HYJHousekeepingEntities hyjhouse = new HYJHousekeepingEntities())
            {
                var employee = hyjhouse.Employees.SingleOrDefault(e => e.EmployeeID == id);
                return employee;
            }
        }
        /// <summary>
        /// 新增员工信息
        /// </summary>
        /// <returns>bool</returns>
        public bool AddEmployee(Employee employee)
        {
            bool result = false;
            using (HYJHousekeepingEntities hyjhouse = new HYJHousekeepingEntities())
            {
                hyjhouse.Employees.Add(new Employee()
                                        {
                                            EmployeeID = employee.EmployeeID,
                                            Password = employee.Password,
                                            EmployeeName = employee.EmployeeName,
                                            DeptID = employee.DeptID,
                                            Gender = employee.Gender,
                                            Address = employee.Address,
                                            Phone = employee.Phone,
                                            EmployeeType = employee.EmployeeType,
                                            EmployeeState = employee.EmployeeState
                                        });
                if (hyjhouse.SaveChanges() > 0)
                {
                    result = true;
                }
            }
            return result;
        }
        /// <summary>
        /// 查询所有员工信息
        /// </summary>
        /// <returns></returns>
        public List<Employee> GetAllEmployee()
        {
            using (HYJHousekeepingEntities hyjhouse = new HYJHousekeepingEntities())
            {
                List<Employee> list = hyjhouse.Employees.ToList();
                return list;
            }

        }
        /// <summary>
        /// 修改员工信息
        /// </summary>
        /// <param name="employeeID">员工编号</param>
        /// <param name="employee">修改后的员工对象</param>
        /// <returns>bool</returns>
        public bool UpdateEmployee(int employeeID, Employee employee)
        {
            bool result = false;
            using (HYJHousekeepingEntities hyjhouse = new HYJHousekeepingEntities())
            {
                Employee emp = GetEmployeeById(employeeID);
                if (emp != null)
                {
                    emp.EmployeeID = employee.EmployeeID;
                    emp.Password = employee.Password;
                    emp.EmployeeName = employee.EmployeeName;
                    emp.DeptID = employee.DeptID;
                    emp.Gender = employee.Gender;
                    emp.Address = employee.Address;
                    emp.Phone = employee.Phone;
                    emp.EmployeeType = employee.EmployeeType;
                    emp.EmployeeState = employee.EmployeeState;
                }
                if (hyjhouse.SaveChanges() > 0)
                {
                    result = true;
                }
            }
            return result;
        }
        /// <summary>
        ///  删除员工信息
        /// </summary>
        /// <param name="employeeID">员工编号</param>
        /// <returns></returns>
        public bool DelEmployee(int employeeID)
        {
            bool result = false;
            using (HYJHousekeepingEntities hyjhouse = new HYJHousekeepingEntities())
            {
                Employee emp= hyjhouse.Employees.FirstOrDefault(e => e.EmployeeID == employeeID);
                if (emp!=null)
                {
                    hyjhouse.Employees.Remove(emp);//从员工集合中删除
                }
                if (hyjhouse.SaveChanges()>0)
                {
                    result = true;
                }
            }
            return result;
        }
        #endregion
    }
}
